package com.qysoft.dtkj.dtkj_gl.actions.xtwh;


import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.plugin.ehcache.CacheKit;
import com.qysoft.dtkj.dtkj_gl.services.xtwh.RYGLService;
import com.qysoft.rapid.actions.JsonResultInterceptor;
import com.qysoft.rapid.actions.RapidAction;
import com.qysoft.rapid.core.RapidDbConn;
import com.qysoft.rapid.core.RapidDbConnTx;
import com.qysoft.rapid.domain.Bean;
import com.qysoft.rapid.utils.StringUtils;

@Before(JsonResultInterceptor.class)
public class RYGLAction extends RapidAction {

	@Clear(JsonResultInterceptor.class)
	public void index() throws Exception{
		bindCurrentRyxxToPage();
		renderJsp("/WEB-INF/pages/xtwh/rygl.jsp");
	}
	
    //角色权限查询
	@Before(RapidDbConn.class)
	public void queryJSXX() throws Exception{
		List<Bean> rolelist = RYGLService.queryJSXX();
		setJson(rolelist);
	}
    /**
     * 删除人员信息
     * @throws Exception 
     */
    @Before(RapidDbConnTx.class)
    public void delete() throws Exception{
        String id = getPara("id");
        RYGLService.delete(id);
		//重置授权的资源缓存
		CacheKit.remove("permissions",id);
    }

    /**
     * 保存新增人员信息
     * @throws Exception 
     */
    @Before(RapidDbConnTx.class)
    public void saveRYXX() throws Exception{
        Bean ryxx = getBean();
        ryxx.set("id", ryxx.get("username")) ;
        ryxx.set("qx_orgid",ryxx.get("orgid")) ;
        ryxx.set("flag", "Y") ;
        ryxx.clear("orgmc");
        RYGLService.saveRYXX(ryxx);
    }
    
    /**
     * 获得人员信息
     * @throws Exception 
     */
    @Before(RapidDbConn.class)
    public void queryRYXX() throws Exception{
        String id = getPara("id");
        Bean ryxx = RYGLService.queryJGXX(id);
        setJson(ryxx);
    }
    
    @Before(RapidDbConnTx.class)
    public void updateRYXX() throws Exception{
      String id = getPara("id");
        Bean ryxx = getBean();
        ryxx.set("id", id);
        String orgid = getPara("orgid");
        if(orgid !=null||!"".equals(orgid)){
        	  ryxx.set("qx_orgid",orgid) ;
        }else{
        	  ryxx.set("qx_orgid","0") ;
        }
        RYGLService.updateRYXX(ryxx);
    }
    
    /**
     * 将密码初始化为123456
     * @throws Exception 
     */
    @Before(RapidDbConnTx.class)
    public void resetPwd() throws Exception{
        String id = getPara("id");
        Bean ryxx = new Bean();
        ryxx.set("id", id);
        ryxx.set("password", StringUtils.getMD5_SALT("123456"));
        RYGLService.updateMmRYXX(ryxx);
    }
	
    /**
     * 保存人员授权角色
     * @throws Exception
     */
	@Before(RapidDbConnTx.class)
	public void saveRyJs() throws Exception{
		String userid = getPara("userid");
		String jsids = getPara("jsids");
		RYGLService.saveRyJs(userid, jsids);
		//重置当前授权的资源缓存
		CacheKit.remove("permissions", userid);
	}
	
	/**
	 * 跳转到修改登录密码页面
	 * @throws Exception
	 */
	@Clear(JsonResultInterceptor.class)
	@Before(RapidDbConnTx.class)
	public void updateXGMM() throws Exception {
		String judge = getPara("judge");
		setAttr("judge",judge);
		renderJsp("/WEB-INF/pages/xtwh/upd_mm.jsp");
	}
	/**
	 * wyg
	 * 20160225
	 * 修改增加企业级登录修改密码
	 * 修改密码信息
	 * @throws Exception
	 */
	@Before(RapidDbConnTx.class)
	public void upd_Mm() throws Exception {
	    String flag;
	    String judge=   getPara("judge");
		Bean bean = getBean();
        String newpwd1=   getPara("newpwd");
        String newpwd= RYGLService.getFromBase64(newpwd1);
        String oldpwd1=   getPara("oldpwd");
        String oldpwd= RYGLService.getFromBase64(oldpwd1);
    	System.out.println("--1--"+newpwd);
    	System.out.println("--0--"+oldpwd);
    	
        String oldmm=     StringUtils.getMD5_SALT(oldpwd);
        if(judge=="1"||judge.equals("1")){
		  String  str = RYGLService.queryYmm(getCurrentRyxx().getId());
		   if(oldmm.equals(str)||oldmm==str){
        	bean.set("id",getCurrentRyxx().getId());
        	bean.set("password", StringUtils.getMD5_SALT(newpwd));
        	RYGLService.updateMM(bean);//人员登录修改密码
		     }
		   flag="0";
        } else{
        	System.out.println("原密码输入不正确!");
        	  flag="1";
        }
		    setJson(flag);//返回到页面的值
		
	}

	
}
